package com.itboye.ihomebank.activity.finance.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.P2PQueryBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.custom.MyListView;
import com.itboye.ihomebank.activity.finance.MyTouZiActivity;
import com.itboye.ihomebank.activity.finance.adapter.TouZiAdapter;
import com.itboye.ihomebank.activity.finance.adapter.TouZiAdapterTwo;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.ByAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 散标投资
 */
public class SanBiaoFragemnt extends BaseFragment implements View.OnClickListener,Observer {
    MyListView youxuan_listView01;
    MyListView youxuan_listView02;
    TouZiAdapter adapter;
    TouZiAdapterTwo adapter02;
    TextView youxuan_moren,youxuan_pingjun,youxuan_qixian;

    List<P2PQueryBean.A> list;
    List<P2PQueryBean.A> list02;
    PtrFrameLayout commend_anchor_ptr;

    JinRongPresenter persenter;
    int pageIndex = 1;

    int type=0;//排序,0=>默认(时间大小),1=>年化大小,2=>年化小大,3=>周期大小,4=>周期小大

    int dian=0;//点击次数

    TextView tvXiangmu;
    @Override
    public int initView() {
        return R.layout.fragemnt_san_biao;
    }



    @Override
    public void onMyActivityCreated() {
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        persenter = new JinRongPresenter(this);
        showProgressDialog("数据载入中,请稍后...", true);
        persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
        persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
        list = new ArrayList<P2PQueryBean.A>();
        list02= new ArrayList<P2PQueryBean.A>();
        youxuan_pingjun.setTag(false);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
            }
        });

        youxuan_listView01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),MyTouZiActivity.class);
                intent.putExtra("id",list.get(position).getId());
                startActivity(intent);
            }
        });
        youxuan_listView02.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),MyTouZiActivity.class);
                intent.putExtra("id",list02.get(position).getId());
                intent.putExtra("type","5");
                startActivity(intent);
            }
        });
    }


    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == JinRongPresenter.p2p_query_success) {
                if(pageIndex==1){
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                P2PQueryBean bean= (P2PQueryBean) entity.getData();
                list.addAll(bean.getList());
                if (bean.getList().size() > 0) {
                    adapter=new TouZiAdapter(getActivity(),list);
                    youxuan_listView01.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }
            }
            if (entity.getEventType()==JinRongPresenter.p2p_query_fail){
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == JinRongPresenter.p2p_query02_success) {
                if(pageIndex==1){
                    list02.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                P2PQueryBean bean= (P2PQueryBean) entity.getData();
                list02.addAll(bean.getList());
                tvXiangmu.setVisibility(View.GONE);
                if (bean.getList().size() > 0) {
                    tvXiangmu.setVisibility(View.VISIBLE);
                    adapter02 = new TouZiAdapterTwo(getActivity(), list02);
                    youxuan_listView02.setAdapter(adapter02);
                    adapter02.notifyDataSetChanged();
                }
            }
            if (entity.getEventType()==JinRongPresenter.p2p_query02_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.youxuan_moren:
             //   clearDrawable();
                break;
            case R.id.youxuan_pingjun:
            //    clearDrawable();
                if (!(boolean) v.getTag()) {
                    type=1;
                    Drawable nav_pingjun=getResources().getDrawable(R.drawable.sanjiao_xia);
                    nav_pingjun.setBounds(0, 0, nav_pingjun.getMinimumWidth(), nav_pingjun.getMinimumHeight());
                    youxuan_pingjun.setCompoundDrawables(null, null, nav_pingjun, null);
                    persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                    persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
                } else {
                    type=2;
                    Drawable nav_pingjun=getResources().getDrawable(R.drawable.sanjiao_shang);
                    nav_pingjun.setBounds(0, 0, nav_pingjun.getMinimumWidth(), nav_pingjun.getMinimumHeight());
                    youxuan_pingjun.setCompoundDrawables(null, null, nav_pingjun, null);
                    persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                    persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
                }
                v.setTag(!(boolean) v.getTag());
                break;
            case R.id.youxuan_qixian:
                dian++;

                if(dian%2!=0){
                    type=3;
                    Drawable nav_pingjun=getResources().getDrawable(R.drawable.sanjiao_xia);
                    nav_pingjun.setBounds(0, 0, nav_pingjun.getMinimumWidth(), nav_pingjun.getMinimumHeight());
                    youxuan_qixian.setCompoundDrawables(null, null, nav_pingjun, null);
                    persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                    persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
                }else{
                    type=4;
                    Drawable nav_pingjun=getResources().getDrawable(R.drawable.sanjiao_shang);
                    nav_pingjun.setBounds(0, 0, nav_pingjun.getMinimumWidth(), nav_pingjun.getMinimumHeight());
                    youxuan_qixian.setCompoundDrawables(null, null, nav_pingjun, null);
                    persenter.listP2p(pageIndex + "", "10", "1", "6386",type+"");
                    persenter.listP2p02(pageIndex + "", "10", "2", "6386",type+"");
                }
                break;
        }
    }

    void clearDrawable(){
        Drawable nav_up=getResources().getDrawable(R.drawable.sanjiao_shang);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        youxuan_pingjun.setCompoundDrawables(null, null, nav_up, null);
        youxuan_qixian.setCompoundDrawables(null, null, nav_up, null);
    }
}
